<template>
  <div class="register-container">
    <!-- 左侧品牌区域 -->
    <div class="brand-section">
      <div class="brand-content">
        <h2 class="brand-title">用户管理系统</h2>
        <p class="brand-slogan">创建您的账号，开启便捷的企业管理之旅。安全、高效、智能的一站式解决方案。</p>
      </div>
    </div>
    
    <!-- 右侧注册表单 -->
    <div class="form-section">
      <el-card class="register-card">
        <template #header>
          <div class="card-header">
            <span>用户注册</span>
          </div>
        </template>
      <el-form 
        ref="registerFormRef" 
        :model="registerForm" 
        :rules="rules" 
        label-width="80px"
        class="register-form"
      >
        <el-form-item label="用户名" prop="username">
          <el-input v-model="registerForm.username" placeholder="请输入用户名"></el-input>
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="registerForm.email" placeholder="请输入邮箱"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input 
            v-model="registerForm.password" 
            type="password" 
            placeholder="请输入密码" 
            show-password
          ></el-input>
        </el-form-item>
        <el-form-item label="确认密码" prop="confirmPassword">
          <el-input 
            v-model="registerForm.confirmPassword" 
            type="password" 
            placeholder="请再次输入密码" 
            show-password
          ></el-input>
        </el-form-item>
        <el-form-item label="姓名">
          <el-row :gutter="10">
            <el-col :span="12">
              <el-input v-model="registerForm.firstName" placeholder="姓氏"></el-input>
            </el-col>
            <el-col :span="12">
              <el-input v-model="registerForm.lastName" placeholder="名字"></el-input>
            </el-col>
          </el-row>
        </el-form-item>
        <el-form-item>
          <el-button 
            type="primary" 
            @click="handleRegister" 
            :loading="loading"
            style="width: 100%"
          >
            注册
          </el-button>
        </el-form-item>
        <div class="form-footer">
          <span>已有账号？</span>
          <router-link to="/login" class="login-link">立即登录</router-link>
        </div>
      </el-form>
    </el-card>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import type { FormInstance, FormRules } from 'element-plus'
import type { RegisterRequest } from '../types/user'
import authService from '../services/auth.service'

const router = useRouter()
const registerFormRef = ref<FormInstance>()
const loading = ref(false)

// 注册表单数据 - 使用自定义类型包含confirmPassword
interface RegisterFormData extends RegisterRequest {
  confirmPassword: string;
}

const registerForm = ref<RegisterFormData>({
  username: '',
  email: '',
  password: '',
  firstName: '',
  lastName: '',
  confirmPassword: '',
})

// 表单验证规则
const rules = ref<FormRules>({
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 3, max: 20, message: '用户名长度在 3 到 20 个字符', trigger: 'blur' }
  ],
  email: [
    { required: true, message: '请输入邮箱', trigger: 'blur' },
    { type: 'email', message: '请输入正确的邮箱格式', trigger: ['blur', 'change'] }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, max: 40, message: '密码长度在 6 到 40 个字符', trigger: 'blur' }
  ],
  confirmPassword: [
    { required: true, message: '请确认密码', trigger: 'blur' },
    {
      validator: (_rule, value, callback) => {
        if (value !== registerForm.value.password) {
          callback(new Error('两次输入的密码不一致'))
        } else {
          callback()
        }
      },
      trigger: 'blur'
    }
  ]
})

// 处理注册
const handleRegister = async () => {
  // 验证表单
  if (!registerFormRef.value) return
  try {
    await registerFormRef.value.validate()
    loading.value = true
    
    // 调用注册接口 - 移除confirmPassword属性
    const { confirmPassword, ...registerData } = registerForm.value;
    await authService.register(registerData)
    
    ElMessage.success('注册成功，请登录')
    router.push('/login')
  } catch (error: any) {
    console.error('Register failed:', error)
    ElMessage.error(
      error.response?.data || error.message || '注册失败，请重试'
    )
  } finally {
    loading.value = false
  }
}
</script>

<style scoped>
/* 主要颜色变量 */
:root {
  --primary-blue: #3498db;
  --primary-blue-light: #66b1ff;
  --primary-blue-dark: #2980b9;
  --primary-blue-ultralight: #e3f2fd;
  --text-primary: #2c3e50;
  --text-secondary: #7f8c8d;
  --background-light: #f8f9fa;
  --border-radius: 16px;
  --shadow: 0 10px 30px rgba(0, 0, 0, 0.08);
  --transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}

/* 注册容器 */
.register-container {
  display: flex;
  height: 100vh;
  background: linear-gradient(135deg, var(--primary-blue) 0%, var(--primary-blue-dark) 100%);
  overflow: hidden;
  position: relative;
}

/* 背景装饰元素 */
.register-container::before {
  content: '';
  position: absolute;
  top: -100px;
  right: -100px;
  width: 400px;
  height: 400px;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.1);
  filter: blur(50px);
}

.register-container::after {
  content: '';
  position: absolute;
  bottom: -100px;
  left: -100px;
  width: 300px;
  height: 300px;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.15);
  filter: blur(50px);
}

/* 左侧品牌区域 */
.brand-section {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 40px;
  color: white;
  position: relative;
  z-index: 1;
  animation: fadeInLeft 0.8s ease-out;
}

.brand-content {
  max-width: 400px;
}

.brand-title {
  font-size: 3rem;
  font-weight: 700;
  margin: 0 0 20px 0;
  text-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.brand-slogan {
  font-size: 1.2rem;
  opacity: 0.9;
  margin: 0 0 40px 0;
  line-height: 1.6;
}

/* 右侧注册表单容器 */
.form-section {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  background: white;
  position: relative;
}

/* 注册卡片 */
.register-card {
  width: 450px;
  max-width: 90%;
  background: white;
  border-radius: var(--border-radius);
  box-shadow: var(--shadow);
  border: none;
  overflow: hidden;
  animation: fadeInRight 0.8s ease-out;
}

/* 卡片头部 */
.card-header {
  text-align: center;
  padding: 32px 32px 0 32px;
}

.card-header span {
  font-size: 2rem;
  font-weight: 700;
  color: var(--text-primary);
}

/* 注册表单 */
.register-form {
  padding: 32px;
}

/* 表单输入框 */
.el-input {
  border-radius: 8px;
  transition: var(--transition);
}

.el-input__wrapper {
  border-radius: 8px;
  border: 1px solid #e0e0e0;
  transition: var(--transition);
  height: 48px;
}

.el-input__wrapper:hover {
  border-color: var(--primary-blue-light);
  box-shadow: 0 2px 8px rgba(52, 152, 219, 0.1);
}

.el-input__wrapper.is-focus {
  border-color: var(--primary-blue);
  box-shadow: 0 0 0 2px rgba(52, 152, 219, 0.1);
}

/* 表单标签 */
.el-form-item__label {
  color: var(--text-secondary);
  font-weight: 500;
  font-size: 14px;
}

/* 注册按钮 */
.el-button--primary {
  width: 100%;
  height: 48px;
  border-radius: 8px;
  background: var(--primary-blue);
  border: none;
  color: white;
  font-size: 16px;
  font-weight: 600;
  transition: var(--transition);
  box-shadow: 0 4px 12px rgba(52, 152, 219, 0.2);
}

.el-button--primary:hover:not(:disabled) {
  background: var(--primary-blue-dark);
  transform: translateY(-1px);
  box-shadow: 0 6px 20px rgba(52, 152, 219, 0.3);
  color: white;
}

.el-button--primary:disabled {
  opacity: 0.6;
}

/* 表单底部 */
.form-footer {
  text-align: center;
  margin-top: 24px;
  font-size: 14px;
  color: var(--text-secondary);
}

.login-link {
  color: var(--primary-blue);
  text-decoration: none;
  font-weight: 500;
  transition: var(--transition);
}

.login-link:hover {
  color: var(--primary-blue-dark);
}

/* 动画 */
@keyframes fadeInLeft {
  from {
    opacity: 0;
    transform: translateX(-30px);
  }
  to {
    opacity: 1;
    transform: translateX(0);
  }
}

@keyframes fadeInRight {
  from {
    opacity: 0;
    transform: translateX(30px);
  }
  to {
    opacity: 1;
    transform: translateX(0);
  }
}

/* 响应式设计 */
@media (max-width: 768px) {
  .register-container {
    flex-direction: column;
    height: auto;
    min-height: 100vh;
  }
  
  .brand-section {
    flex: 0;
    padding: 60px 20px 30px 20px;
  }
  
  .brand-title {
    font-size: 2rem;
    text-align: center;
  }
  
  .brand-slogan {
    font-size: 1rem;
    text-align: center;
  }
  
  .form-section {
    flex: 1;
    padding: 40px 20px;
    width: 100%;
  }
  
  .register-card {
    width: 100%;
  }
  
  .card-header {
    padding: 24px 24px 0 24px;
  }
  
  .card-header span {
    font-size: 1.6rem;
  }
  
  .register-form {
    padding: 24px;
  }
  
  .form-footer {
    margin-top: 16px;
  }
}
</style>